namespace Digitalmes.Domain.Entities.Mes.Equipments;

/// <summary>
/// 设备警报记录。
/// </summary>
[SugarTable("equip_alarm_record")]
public sealed class EquipAlarmRecord : EntityBase
{
    /// <summary>
    /// 产线代码
    /// </summary>
    [NotNull]
    public string? LineCode { get; set; }

    /// <summary>
    /// 工作中心（区域/工位/设备）
    /// </summary>
    [NotNull]
    public string? WorkCenter { get; set; }

    /// <summary>
    /// 警报顺序号，从 1 开始。
    /// </summary>
    public int Index { get; set; }

    /// <summary>
    /// 警报内容
    /// </summary>
    [NotNull]
    public string? Content { get; set; }

    /// <summary>
    /// 警报分类
    /// </summary>
    public string? Category { get; set; }

    /// <summary>
    /// 警报级别
    /// </summary>
    public AlarmLevelEnum AlarmLevel { get; set; }

    /// <summary>
    /// 警报开始时间
    /// </summary>
    public DateTime StartTime { get; set; }

    /// <summary>
    /// 警报结束时间
    /// </summary>
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 警报时长 (分钟)
    /// </summary>
    public double Duration { get; set; }

    /// <summary>
    /// 警报状态（1->警报中; 2->已关闭）
    /// </summary>
    public AlarmStatusEnum Status { get; set; }

    /// <summary>
    /// 警报关闭
    /// </summary>
    public void Close()
    {
        EndTime = DateTime.Now;
        Status = AlarmStatusEnum.Closed;
        Duration = Math.Round((EndTime! - StartTime).Value.TotalMinutes, 1);
    }
}
